// import { CfgItemID } from "../configs/CfgItem";
import Cfgs from "../configs/Cfgs";
// import { IReward } from "../configs/CfgSteakCollection";
import LocalValues, { LocalKey } from "../data/LocalValues";
import UserCtrl from "../data/UserCtrl";
import UserModel from "../data/UserModel";
import { EventReport } from "../net/EventReport";
// import HallCtrl from "./HallCtrl";
import { BaseConfig } from "../base/BaseConfig";

class PlaceGiftsCtrl {

    public get cfg() {
        // return Cfgs.PlaceGifts;
        return new BaseConfig<any>() as any;
    }

    /**是否解锁 */
    public isUnlock() {
        // return UserModel.curLevelId >= this.cfg.unlockLevel;
        return true;
    }

    /**获得下次领取时间，0表示可以领取 */
    public getTimeLeft() {
        const lastTime = LocalValues.getNumber(LocalKey.placeGiftsReceiveTime);
        if (!lastTime) {
            return 0;
        }
        const intervalTime = this.cfg.intervalTime;
        const now = Date.now();
        const diff = now - lastTime;
        if (diff > intervalTime) {
            return 0;
        }
        return intervalTime - diff;
    }
    /**获得本次奖励 */
    public getRewards() {
        let rewards = [...this.cfg.commonReward];
        const level = UserModel.curLevelId - this.cfg.unlockLevel;
        const levelCount = Math.floor(level / this.cfg.levelDemand);
        for (let i = 0; i < levelCount; i++) {
            rewards.push(...this.cfg.levelReward);
        }
        // rewards = this.margeRewards(rewards);
        const mostReward = this.cfg.mostReward;
        mostReward.forEach(v => {
            const reward = rewards.find(v2 => v2.itemId === v.itemId);
            if (reward && reward.count > v.count) {
                reward.count = v.count;
            }
        });
        return rewards;
    }

    public hasReward(): boolean {
        const timeLeft = this.getTimeLeft();
        return timeLeft <= 0; // 如果时间归零，表示有奖励可领取
    }
    public receive(multiple: number = 1, bAd: boolean = false) {
        // multiple = multiple || 1;
        // LocalValues.setNumber(LocalKey.placeGiftsReceiveTime, Date.now());
        // const reward = this.getRewards()[0];
        // if (reward.itemId == CfgItemID.coin) {
        //     UserCtrl.addUserItem(reward, false);
        //     HallCtrl.popCoin(reward.count * multiple, bAd);
        //     UserCtrl.reportDeliver(reward, 7);
        // } else {
        //     HallCtrl.addRewardToQueue([reward], 7);
        // }
        // EventReport.countdown_gift({
        //     level: UserModel.curLevelId,
        //     layoutid: UserModel.curLevelId,
        //     countdownCoin: reward.count * multiple,
        //     isDouble: multiple > 1,
        //     countdownGiftTimes: LocalValues.addCount('_countdownGiftTimes'),
        // });
    }

    // private margeRewards(rewards: IReward[]) {
    //     const map: { [key: string]: number } = {};
    //     for (let i = 0; i < rewards.length; i++) {
    //         const reward = rewards[i];
    //         if (map[reward.itemId]) {
    //             map[reward.itemId] += reward.count;
    //         } else {
    //             map[reward.itemId] = reward.count;
    //         }
    //     }
    //     // 将map转为数组
    //     const result: IReward[] = [];
    //     for (const key in map) {
    //         const itemId = parseInt(key);
    //         const count = map[key];
    //         if (count) {
    //             result.push({ itemId: itemId, count: count, time: 0 });
    //         }
    //     }
    //     return result;
    // }

}

export default new PlaceGiftsCtrl();
